#!/bin/bash

###############################################################################
#Copyright (c) 2006-2008 Innobox Systems Private Limited
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions
#are met:
#1. Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
#2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
#3. Neither the name of the author nor the names of its contributors
#   may not be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
#IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
#OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
#NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
#THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################
###############################################################################
#### Please inform any bugs or improvements to opensource@innobox.com #########
###############################################################################
################################ Notes ########################################
#
# This script generates stub functions and stub variables
# When we want to remove a library from existing binary, 
# and not sure what is used from that library, the script can be used
# to generate the stub code and functions which can be used as starting point
######## version - 0.1 ########################################################
#     -  initial version
######## version - 0.2 ########################################################
#     -  Added the initial comments  and description
###############################################################################
IBX_VERSION=0.2
################################# functions ###################################
function ibxFileAppend()
{
  _file=$1;shift
  echo $* >> $_file
}

################################# start here ###################################
IBX_CHECK_BINARY=1
IBX_GOOD_BINARY=$4
IBX_COMP_ERRORS=$1
IBX_OUTPUT=$2
IBX_INC_FILE=$3
IBX_DATA_SECTION_LOG=data.log
IBX_TEXT_SECTION_LOG=text.log
IBX_BSSS_SECTION_LOG=bsss.log
IBX_OUTPUT_DATA=$IBX_OUTPUT"_stub_data.c"
IBX_OUTPUT_CODE=$IBX_OUTPUT"_stub.c"

IBX_UNDEF_SYMBOLS=`cat $IBX_COMP_ERRORS | grep "Undefined symbol" | grep "'" | cut -d "'" -f2 | sort | uniq`

if [ -n "$IBX_CHECK_BINARY" ]; then
  objdump.exe --section=.data -t $IBX_GOOD_BINARY | sed 's/[ \t]\+/ /g' | sed 's/$/ /g' > $IBX_DATA_SECTION_LOG
  objdump.exe --section=.text -t $IBX_GOOD_BINARY | sed 's/[ \t]\+/ /g' | sed 's/$/ /g' > $IBX_TEXT_SECTION_LOG
  objdump.exe --section=.bss  -t $IBX_GOOD_BINARY | sed 's/[ \t]\+/ /g' | sed 's/$/ /g' > $IBX_BSSS_SECTION_LOG
fi

rm -f $IBX_OUTPUT_CODE $IBX_OUTPUT_DATA
(
cat <<IBX_CODE_BLK_END

/**********************************************************************
 *
 * $IBX_OUTPUT_DATA
 *
 * This file was originally auto-generated by ibxGetUndef.sh
 * part of ibxDevScripts - free scripts by Innobox Systems Pvt Ltd.
 *
 **********************************************************************/

/* variables */
IBX_CODE_BLK_END
) >> $IBX_OUTPUT_DATA

if [ -n "$IBX_CHECK_BINARY" ]; then
for i in $IBX_UNDEF_SYMBOLS; do
  size=`grep " $i " $IBX_DATA_SECTION_LOG | cut -d " " -f5`
  test -n "$size" || continue
  ibxFileAppend $IBX_OUTPUT_DATA "char $i[0x$size];"
done

for i in $IBX_UNDEF_SYMBOLS; do
  size=`grep " $i " $IBX_BSSS_SECTION_LOG | cut -d " " -f5`
  test -n "$size" || continue
  ibxFileAppend $IBX_OUTPUT_DATA "char $i[0x$size];"
done
fi

(
cat <<IBX_CODE_BLK_END >> $IBX_OUTPUT_CODE
/**********************************************************************
 *
 * $IBX_OUTPUT_CODE
 *
 * This file was originally auto-generated by ibxGetUndef.sh
 * part of ibxDevScripts - free scripts by Innobox Systems Pvt Ltd.
 *
 **********************************************************************/

#include "$IBX_INC_FILE"

/* trace function - ovveride it as required */
#ifndef IBX_TRACE_DUMMY
#include <stdio.h>
#define IBX_TRACE_DUMMY() printf("%s-%d\n", __FILE__, __LINE__); return 0;
#endif

/* functions */
IBX_CODE_BLK_END
) >> $IBX_OUTPUT_CODE

for i in $IBX_UNDEF_SYMBOLS; do
  if [ -n "$IBX_CHECK_BINARY" ]; then
    size=`grep " $i " $IBX_TEXT_SECTION_LOG | cut -d " " -f5`
    test -n "$size" || continue
  fi
(
  cat <<-IBX_CODE_BLK_END
int $i(void)
{
  IBX_TRACE_DUMMY();
}

IBX_CODE_BLK_END
) >> $IBX_OUTPUT_CODE
done
